MkText
Class for any Markup text.
Note
Description
All classes inheriting from MkNode can get converted to this Type.
Examples
Example: Regular
Note
Jinja
{{ "Some text" | MkText }}
Note
Python
MkText('Some text')
Rendered
Some text
Markdown
Some text
Html
<p>Some text</p>
Sub classes
| Name | Children | Inherits |
|---|---|---|
| MkInclude mknodes.templatenodes.mkinclude Node to include the text of other Markdown files / MkNodes. |
| |
| MkChangelog mknodes.templatenodes.mkchangelog Node for a git-based changelog (created by git-changelog). |
| |
| MkLicense mknodes.templatenodes.mklicense Node to show a license. |
| |
| MkLlm mknodes.templatenodes.mkllm Node for LLM-based text generation. |
|
Base classes
| Name | Children | Inherits |
|---|---|---|
| TemplateRenderMixin mknodes.jinja.rendercache Renders the node’s template once and serves both children and content from it. | ||
| MkNode mknodes.basenodes.mknode Base class for everything which can be expressed as Markup. |
⋔ Inheritance diagram
graph TD
1315922923["mktext.MkText"]
3160803176["rendercache.TemplateRenderMixin"]
2751680898["builtins.object"]
1359323199["mknode.MkNode"]
3160803176 --> 1315922923
2751680898 --> 3160803176
1359323199 --> 1315922923
2751680898 --> 1359323199
NodeFile
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mktext/metadata.toml
[metadata]
icon = "mdi:text"
name = "MkText"
group = "base"
[examples.regular]
title = "Regular"
jinja = """
{{ "Some text" | MkText }}
"""
# [examples.from_url]
# title = "From URL"
# jinja = """
# {{ mk.MkText.from_url("https://raw.githubusercontent.com/phil65/mknodes/main/README.md") }}
# """
[output.markdown]
template = """
{{ node.get_text() }}
"""
Code
mknodes.basenodes.mktext.MkText
class MkText(TemplateRenderMixin, mknode.MkNode):
"""Class for any Markup text.
All classes inheriting from MkNode can get converted to this Type.
"""
ICON = "material/text"
def __init__(
self,
text: str | mknode.MkNode | None = "",
*,
render_jinja: bool = False,
**kwargs: Any,
) -> None:
"""Constructor.
Args:
text: Markup text
render_jinja: Whether text should get rendered by this node
kwargs: Keyword arguments passed to parent
"""
super().__init__(**kwargs)
self._text = str(text or "")
self.render_jinja = render_jinja
def get_section(self, section_name: str) -> Self | None:
markdown = self.get_text()
section_text = mdfilters.extract_header_section(markdown, section_name)
return None if section_text is None else type(self)(section_text)
def render_kwargs(self) -> dict[str, Any]:
return dict(string=self._text, variables=self.variables)
def get_text(self) -> str:
if not self.render_jinja:
return self._text
return (self.current_render_result()).markdown
def set_text(self, value: str) -> None:
self._text = value
def to_md_unprocessed(self) -> str:
return self.get_text()
def get_children(self) -> list[mknode.MkNode]:
"""Return children nodes.
For MkText with render_jinja=True, this triggers Jinja rendering
and returns any nodes created during rendering.
"""
if not self.render_jinja:
return []
return self.get_render_result().children
@classmethod
def from_url(cls, url: str) -> Self | None:
"""Build a MkText node on a remote markup file.
If the URL contains a "#" (http://.../markdown.md#section),
it will try to extract the given section.
All fsspec protocols are supported as URL.
Args:
url: URL to get markdown from.
"""
url, *section = url.split("#")
text = pathhelpers.load_file_cached(url)
if not section:
return cls(text)
extracted = mdfilters.extract_header_section(text, section[0])
return cls(extracted) if extracted is not None else None
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes
def create_section_for_nodes(
nav: mk.MkNav,
klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
for kls in klasses:
tpl = "classpage_custom.jinja"
page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
nav += page
if kls.STATUS:
page.metadata.status = kls.STATUS
elif kls.JS_FILES:
page.metadata.status = "js"
elif kls.CSS:
page.metadata.status = "css"
page.created_by = create_section_for_nodes
link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
table.add_row((link, kls.__doc__, extensions))
return table
Resources
Resources(css=[],
markdown_extensions={'admonition': {},
'pymdownx.details': {},
'pymdownx.highlight': {'anchor_linenums': True,
'line_spans': '__span',
'pygments_lang_class': True},
'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
'format': <function fence_code_format at 0x7d707a397060>,
'name': 'mermaid'}]},
'pymdownx.tabbed': {},
'tables': {}},
plugins=[Plugin('mkdocstrings')],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
source_function: create_section_for_nodes
source_line_no: 93
hide:
- toc
icon: material/text
title: MkText